import UIKit
import GradientLoadingBar
import Alamofire
import Reachability


class AssistantViewController: UIViewController {
    let cell_identifier:String = "assistantCell"
    var list:[Assistant] = []
    var myNavigationController: UINavigationController?
    // 懒加载
    lazy var tableView : UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        
        let itemW = (UIScreen.main.bounds.width - 32)
        layout.itemSize = CGSize(width: itemW, height: 80)
        
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.minimumLineSpacing = 12
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .vertical // 竖向滑动
        
        
        let tableView = UICollectionView(frame: CGRectMake(0, 32, UIScreen.main.bounds.width, UIScreen.main.bounds.height), collectionViewLayout: layout)
        tableView.showsVerticalScrollIndicator = false
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = primaryDarkBackgroundColor
        tableView.register(AssistantCell.self, forCellWithReuseIdentifier: cell_identifier)
        
        return tableView
    }()
    override var wx_titleTextAttributes: [NSAttributedString.Key : Any]? {
           return [NSAttributedString.Key.foregroundColor: UIColor.white]
       }
  
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(tableView)
        self.view.backgroundColor = primaryDarkBackgroundColor
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.view).offset(32)
            make.bottom.equalTo(self.view)
            make.width.equalTo(self.view)
        }
    }
    
    
    
}

extension AssistantViewController: UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return list.count
    }
    

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cell_identifier, for: indexPath) as! AssistantCell
        cell.bindViewModel(list[indexPath.row])
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.item + 1)
        let chatViewController = AdvancedExampleViewController()
        let assistant = list[indexPath.row]
        chatViewController.roleDescription = assistant.subTitle
        chatViewController.pageTitle = assistant.title
        chatViewController.rolePrompt = assistant.prompt
        chatViewController.inputHint = assistant.hintTitle
        chatViewController.type = assistant.type
//        chatViewController.example = assistant.example
//        chatViewController.exampleAnswer = assistant.answer
        self.myNavigationController?.pushViewController(chatViewController, animated: true)
        
        
        
    }
    
}
